package com.ymatou.iapi.productmanager.testcase;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.productmanager.parameter.UpdateProductCatalogDto;
import com.ymatou.iapi.productmanager.parameter.UpdateProductCatalogsBean;
import com.ymatou.iapi.productmanager.service.UpdateProductCatalogsCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.NewJavaMongoCheckService;
import com.ymttest.business.service.SellerProductCallService;
import com.ymttest.business.service.productdata.impl.ProductFactory;
import com.ymttest.database.model.YmtCatalogsI;
import com.ymttest.database.mongo.YmtMongoDriverFactroy;
import com.ymttest.database.sqlwapper.YmtCatalogsIWapper;
import com.ymttest.database.sqlwapper.YmtProductsIWapper;
import com.ymttest.utils.EnvSetup;

public class Ts_UpdateProductCatalogs {
	private static UpdateProductCatalogsCall updateProductCatalogsCall = new UpdateProductCatalogsCall();
	private static Integer sellerId = Integer.valueOf(EnvSetup.getData("productsellerid"));
	private static String sellerName = EnvSetup.getData("productsellername");	
	private static YmtCatalogsIWapper ymtCatalogsIWapper = new YmtCatalogsIWapper();
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("更新规格库存接口");
	}
	
	@Before
	public void caseUp() {
	}
	
	@AfterClass
	public static void tearDown() {
		Logger.generateResult(updateProductCatalogsCall.getOpurl());
	}
	
	@After
	public void caseDown() {
		Logger.end();
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_UpdateProductCatalogs_001() {
		Logger.start(true,"多规格普通商品修改价格和库存,包含num,newprice,vipprice,为0场景");
		try{
			String productId = ProductFactory.getOnlyProduct(sellerId, sellerName).getProductId();
			
			List<String> catalogIds = ymtCatalogsIWapper.selectCatalogsByProductId(productId).
											stream().map(x->x.getsCatalogId()).collect(Collectors.toList());
			
			
			//批量修改，num,newprice,vipprice,为0场景
			UpdateProductCatalogsBean updateCatalogBean = new UpdateProductCatalogsBean();
			updateCatalogBean.setProductID(productId);
			updateCatalogBean.setUserID(sellerId);
			
			UpdateProductCatalogDto updateCatalogDto = new UpdateProductCatalogDto();
			updateCatalogDto.setCatalogId(catalogIds.get(0));
			updateCatalogDto.setUserId(sellerId);
			updateCatalogDto.setPrice(50);
			
			
			UpdateProductCatalogDto updateCatalogDto2 = new UpdateProductCatalogDto();
			updateCatalogDto2.setCatalogId(catalogIds.get(1));
			updateCatalogDto2.setUserId(sellerId);
			updateCatalogDto2.setPrice(41);
			
			updateCatalogBean.setCatalogs(Arrays.asList(updateCatalogDto,updateCatalogDto2));
			
			JSONObject ret = updateProductCatalogsCall.callAndGetReturnData(updateCatalogBean);
			Logger.verifyEquals(200, ret.getInt("Code"), "返回码应为200");
			checkPriceAndStock(updateCatalogBean);
			
			//===================================================================================================================
			//批量修改，num,newprice,vipprice,都不为0场景
			updateCatalogDto.setNum(20);
			updateCatalogDto.setNewCustomerPrice(49);
			updateCatalogDto.setVipPrice(48);
			
			updateCatalogDto2.setNum(19);
			updateCatalogDto2.setNewCustomerPrice(39);
			updateCatalogDto2.setVipPrice(38);
			
			ret = updateProductCatalogsCall.callAndGetReturnData(updateCatalogBean);
			Logger.verifyEquals(200, ret.getInt("Code"), "返回码应为200");
			checkPriceAndStock(updateCatalogBean);
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_UpdateProductCatalogs_002() {
		Logger.start(true,"多规格活动商品修改价格和库存,包含num,newprice,vipprice,为0场景");
		try{
			String productId = ProductFactory.getActivityProduct(sellerId, sellerName).getProductId();
			
			List<String> catalogIds = ymtCatalogsIWapper.selectCatalogsByProductId(productId).
											stream().map(x->x.getsCatalogId()).collect(Collectors.toList());
			
			
			//批量修改，num,newprice,vipprice,为0场景
			UpdateProductCatalogsBean updateCatalogBean = new UpdateProductCatalogsBean();
			updateCatalogBean.setProductID(productId);
			updateCatalogBean.setUserID(sellerId);
			
			UpdateProductCatalogDto updateCatalogDto = new UpdateProductCatalogDto();
			updateCatalogDto.setCatalogId(catalogIds.get(0));
			updateCatalogDto.setUserId(sellerId);
			updateCatalogDto.setPrice(50);
			
			
			UpdateProductCatalogDto updateCatalogDto2 = new UpdateProductCatalogDto();
			updateCatalogDto2.setCatalogId(catalogIds.get(1));
			updateCatalogDto2.setUserId(sellerId);
			updateCatalogDto2.setPrice(41);
			
			updateCatalogBean.setCatalogs(Arrays.asList(updateCatalogDto,updateCatalogDto2));
			
			JSONObject ret = updateProductCatalogsCall.callAndGetReturnData(updateCatalogBean);
			Logger.verifyEquals(200, ret.getInt("Code"), "返回码应为200");
			checkPriceAndStock(updateCatalogBean);
			
			//===================================================================================================================
			//批量修改，num,newprice,vipprice,都不为0场景
			updateCatalogDto.setNum(20);
			updateCatalogDto.setNewCustomerPrice(49);
			updateCatalogDto.setVipPrice(48);
			
			updateCatalogDto2.setNum(19);
			updateCatalogDto2.setNewCustomerPrice(39);
			updateCatalogDto2.setVipPrice(38);
			
			ret = updateProductCatalogsCall.callAndGetReturnData(updateCatalogBean);
			Logger.verifyEquals(200, ret.getInt("Code"), "返回码应为200");
			checkPriceAndStock(updateCatalogBean);
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_UpdateProductCatalogs_003() {
		Logger.start(true,"多规格FBX商品修改价格和库存,只能修改价格");
		try{
			String productId = ProductFactory.getOnlyProduct(sellerId, sellerName).getProductId();
			new YmtProductsIWapper().updateCatalogTypeByProductId(productId, 2);
			
			List<String> catalogIds = ymtCatalogsIWapper.selectCatalogsByProductId(productId).
											stream().map(x->x.getsCatalogId()).collect(Collectors.toList());
			
			
			//===================================================================================================================
			//批量修改，num,newprice,vipprice,都不为0场景
			UpdateProductCatalogsBean updateCatalogBean = new UpdateProductCatalogsBean();
			updateCatalogBean.setProductID(productId);
			updateCatalogBean.setUserID(sellerId);
			
			UpdateProductCatalogDto updateCatalogDto = new UpdateProductCatalogDto();
			updateCatalogDto.setCatalogId(catalogIds.get(0));
			int origNum = ymtCatalogsIWapper.selectCatalogsByCatalogId(catalogIds.get(0)).getiNum();
			updateCatalogDto.setUserId(sellerId);
			updateCatalogDto.setPrice(50);
			updateCatalogDto.setNum(20);
			updateCatalogDto.setNewCustomerPrice(49);
			updateCatalogDto.setVipPrice(48);
			
			
			UpdateProductCatalogDto updateCatalogDto2 = new UpdateProductCatalogDto();
			updateCatalogDto2.setCatalogId(catalogIds.get(1));
			int origNum2 = ymtCatalogsIWapper.selectCatalogsByCatalogId(catalogIds.get(1)).getiNum();
			updateCatalogDto2.setUserId(sellerId);
			updateCatalogDto2.setPrice(41);
			updateCatalogDto2.setNum(19);
			updateCatalogDto2.setNewCustomerPrice(39);
			updateCatalogDto2.setVipPrice(38);
			updateCatalogBean.setCatalogs(Arrays.asList(updateCatalogDto,updateCatalogDto2));
			
			JSONObject ret = updateProductCatalogsCall.callAndGetReturnData(updateCatalogBean);
			Logger.verifyEquals(200, ret.getInt("Code"), "返回码应为200");
			
			
			//库存不能修改
			updateCatalogDto.setNum(origNum);
			updateCatalogDto2.setNum(origNum2);
			new YmtProductsIWapper().updateCatalogTypeByProductId(productId, 0);
			checkPriceAndStock(updateCatalogBean);
			
			
			
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_UpdateProductCatalogs_004() {
		Logger.start(true,"多规格活动商品修改价格和库存,价格物流限制校验");
		try{
			int sellerId = 3383;
			//价格与物流方式不匹配 无法上架"
			String productId =  SellerProductCallService.addNewProduct5dayago(sellerId, sellerName, null).getJSONObject("Data").getString("ProductID");	
			
			new YmtProductsIWapper().updateCatalogStatus(productId, 7);
			
			List<String> catalogIds = ymtCatalogsIWapper.selectCatalogsByProductId(productId).
											stream().map(x->x.getsCatalogId()).collect(Collectors.toList());
			
			
			
			UpdateProductCatalogsBean updateCatalogBean = new UpdateProductCatalogsBean();
			updateCatalogBean.setProductID(productId);
			updateCatalogBean.setUserID(sellerId);
			
			UpdateProductCatalogDto updateCatalogDto = new UpdateProductCatalogDto();
			updateCatalogDto.setCatalogId(catalogIds.get(0));
			updateCatalogDto.setUserId(sellerId);
			updateCatalogDto.setPrice(2000);
			
		
			updateCatalogBean.setCatalogs(Arrays.asList(updateCatalogDto));
			
			JSONObject ret = updateProductCatalogsCall.callAndGetReturnData(updateCatalogBean);
			Logger.verifyEquals(201, ret.getInt("Code"), "返回码应为201");
			
			
		}catch(Exception e){
			Logger.fail(e);
		}
	}
			
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_UpdateProductCatalogs_888() {
		Logger.start(true,"买手不是该商品发布者,修改失败");
		try{
			String productId = ProductFactory.getOnlyProduct(sellerId, sellerName).getProductId();
			
			List<String> catalogIds = ymtCatalogsIWapper.selectCatalogsByProductId(productId).
											stream().map(x->x.getsCatalogId()).collect(Collectors.toList());
			
			
			//===================================================================================================================
			//批量修改，num,newprice,vipprice,都不为0场景
			UpdateProductCatalogsBean updateCatalogBean = new UpdateProductCatalogsBean();
			updateCatalogBean.setProductID(productId);
			updateCatalogBean.setUserID(sellerId+1);
			
			UpdateProductCatalogDto updateCatalogDto = new UpdateProductCatalogDto();
			updateCatalogDto.setCatalogId(catalogIds.get(0));
			updateCatalogDto.setUserId(sellerId+1);
			updateCatalogDto.setPrice(50);
			updateCatalogDto.setNum(20);
			updateCatalogDto.setNewCustomerPrice(49);
			updateCatalogDto.setVipPrice(48);
			updateCatalogBean.setCatalogs(Arrays.asList(updateCatalogDto));
			
			JSONObject ret = updateProductCatalogsCall.callAndGetReturnData(updateCatalogBean);
			Logger.verifyEquals(201, ret.getInt("Code"), "返回码应为201");
			
			
			//数据未变
			YmtCatalogsI catalogInSql = ymtCatalogsIWapper.selectCatalogsByCatalogId(catalogIds.get(0));
			updateCatalogDto.setPrice(catalogInSql.getfQuotePrice().doubleValue());
			updateCatalogDto.setNum(catalogInSql.getiNum());
			updateCatalogDto.setNewCustomerPrice(catalogInSql.getfNewCustomerPrice().doubleValue());
			updateCatalogDto.setVipPrice(catalogInSql.getfVipPrice().doubleValue());
			checkPriceAndStock(updateCatalogBean);
			
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_UpdateProductCatalogs_889() {
		Logger.start(true,"价格为0,全部修改失败");
		try{
			String productId = ProductFactory.getOnlyProduct(sellerId, sellerName).getProductId();
			
			List<String> catalogIds = ymtCatalogsIWapper.selectCatalogsByProductId(productId).
											stream().map(x->x.getsCatalogId()).collect(Collectors.toList());
			
			
			//===================================================================================================================
			//批量修改，num,newprice,vipprice,都不为0场景
			UpdateProductCatalogsBean updateCatalogBean = new UpdateProductCatalogsBean();
			updateCatalogBean.setProductID(productId);
			updateCatalogBean.setUserID(sellerId);
			
			UpdateProductCatalogDto updateCatalogDto = new UpdateProductCatalogDto();
			updateCatalogDto.setCatalogId(catalogIds.get(0));
			updateCatalogDto.setUserId(sellerId);
			updateCatalogDto.setPrice(0);
			updateCatalogDto.setNum(20);
			updateCatalogDto.setNewCustomerPrice(49);
			updateCatalogDto.setVipPrice(48);
			
			UpdateProductCatalogDto updateCatalogDto2 = new UpdateProductCatalogDto();
			updateCatalogDto2.setCatalogId(catalogIds.get(1));
			updateCatalogDto2.setUserId(sellerId);
			updateCatalogDto2.setPrice(41);
			updateCatalogDto2.setNum(19);
			updateCatalogDto2.setNewCustomerPrice(39);
			updateCatalogDto2.setVipPrice(38);
			updateCatalogBean.setCatalogs(Arrays.asList(updateCatalogDto,updateCatalogDto2));
			
			JSONObject ret = updateProductCatalogsCall.callAndGetReturnData(updateCatalogBean);
			Logger.verifyEquals(400, ret.getInt("Code"), "返回码应为400");
			
			
			//数据未变
			YmtCatalogsI catalogInSql = ymtCatalogsIWapper.selectCatalogsByCatalogId(catalogIds.get(0));
			updateCatalogDto.setPrice(catalogInSql.getfQuotePrice().doubleValue());
			updateCatalogDto.setNum(catalogInSql.getiNum());
			updateCatalogDto.setNewCustomerPrice(catalogInSql.getfNewCustomerPrice().doubleValue());
			updateCatalogDto.setVipPrice(catalogInSql.getfVipPrice().doubleValue());
			
			
			YmtCatalogsI catalogInSql2 = ymtCatalogsIWapper.selectCatalogsByCatalogId(catalogIds.get(1));
			updateCatalogDto2.setPrice(catalogInSql2.getfQuotePrice().doubleValue());
			updateCatalogDto2.setNum(catalogInSql2.getiNum());
			updateCatalogDto2.setNewCustomerPrice(catalogInSql2.getfNewCustomerPrice().doubleValue());
			updateCatalogDto2.setVipPrice(catalogInSql2.getfVipPrice().doubleValue());
			
			checkPriceAndStock(updateCatalogBean);
			
			
			
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_UpdateProductCatalogs_890() {
		Logger.start(true,"一个规格不存在,部分修改失败");
		try{
			String productId = ProductFactory.getOnlyProduct(sellerId, sellerName).getProductId();
			
			List<String> catalogIds = ymtCatalogsIWapper.selectCatalogsByProductId(productId).
											stream().map(x->x.getsCatalogId()).collect(Collectors.toList());
			
			
			//===================================================================================================================
			//批量修改，num,newprice,vipprice,都不为0场景
			UpdateProductCatalogsBean updateCatalogBean = new UpdateProductCatalogsBean();
			updateCatalogBean.setProductID(productId);
			updateCatalogBean.setUserID(sellerId);
			
			UpdateProductCatalogDto updateCatalogDto = new UpdateProductCatalogDto();
			updateCatalogDto.setCatalogId("dsdfsdf");
			updateCatalogDto.setUserId(sellerId);
			updateCatalogDto.setPrice(1);
			updateCatalogDto.setNum(20);
			updateCatalogDto.setNewCustomerPrice(49);
			updateCatalogDto.setVipPrice(48);
			
			UpdateProductCatalogDto updateCatalogDto2 = new UpdateProductCatalogDto();
			updateCatalogDto2.setCatalogId(catalogIds.get(1));
			updateCatalogDto2.setUserId(sellerId);
			updateCatalogDto2.setPrice(41);
			updateCatalogDto2.setNum(19);
			updateCatalogDto2.setNewCustomerPrice(39);
			updateCatalogDto2.setVipPrice(38);
			updateCatalogBean.setCatalogs(Arrays.asList(updateCatalogDto,updateCatalogDto2));
			
			JSONObject ret = updateProductCatalogsCall.callAndGetReturnData(updateCatalogBean);
			Logger.verifyEquals(200, ret.getInt("Code"), "返回码应为200");
			
			
			//不存在的规格 数据未变
			YmtCatalogsI catalogInSql = ymtCatalogsIWapper.selectCatalogsByCatalogId(catalogIds.get(0));
			updateCatalogDto.setPrice(catalogInSql.getfQuotePrice().doubleValue());
			updateCatalogDto.setCatalogId(catalogIds.get(0));
			updateCatalogDto.setNum(catalogInSql.getiNum());
			updateCatalogDto.setNewCustomerPrice(catalogInSql.getfNewCustomerPrice().doubleValue());
			updateCatalogDto.setVipPrice(catalogInSql.getfVipPrice().doubleValue());
			
			checkPriceAndStock(updateCatalogBean);
			
			
			
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_UpdateProductCatalogs_891() {
		Logger.start(true,"商品不存在,修改失败");
		try{
			String productId = ProductFactory.getOnlyProduct(sellerId, sellerName).getProductId();
			
			List<String> catalogIds = ymtCatalogsIWapper.selectCatalogsByProductId(productId).
											stream().map(x->x.getsCatalogId()).collect(Collectors.toList());
			
			
			//===================================================================================================================
			//批量修改，num,newprice,vipprice,都不为0场景
			UpdateProductCatalogsBean updateCatalogBean = new UpdateProductCatalogsBean();
			updateCatalogBean.setProductID("sdfadfadfads");
			updateCatalogBean.setUserID(sellerId);
			
			UpdateProductCatalogDto updateCatalogDto = new UpdateProductCatalogDto();
			updateCatalogDto.setCatalogId(catalogIds.get(0));
			updateCatalogDto.setUserId(sellerId+1);
			updateCatalogDto.setPrice(50);
			updateCatalogDto.setNum(20);
			updateCatalogDto.setNewCustomerPrice(49);
			updateCatalogDto.setVipPrice(48);
			updateCatalogBean.setCatalogs(Arrays.asList(updateCatalogDto));
			
			JSONObject ret = updateProductCatalogsCall.callAndGetReturnData(updateCatalogBean);
			Logger.verifyEquals(201, ret.getInt("Code"), "返回码应为201");
			
			
			//数据未变
			YmtCatalogsI catalogInSql = ymtCatalogsIWapper.selectCatalogsByCatalogId(catalogIds.get(0));
			updateCatalogBean.setProductID(productId);
			updateCatalogDto.setPrice(catalogInSql.getfQuotePrice().doubleValue());
			updateCatalogDto.setNum(catalogInSql.getiNum());
			updateCatalogDto.setNewCustomerPrice(catalogInSql.getfNewCustomerPrice().doubleValue());
			updateCatalogDto.setVipPrice(catalogInSql.getfVipPrice().doubleValue());
			checkPriceAndStock(updateCatalogBean);
			
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	
	
	
	public static void checkPriceAndStock(UpdateProductCatalogsBean updateCatalogBean) throws Exception{
		String productId = updateCatalogBean.getProductID();
		List<YmtCatalogsI> catalogsInSql = ymtCatalogsIWapper.selectCatalogsByProductId(productId);
		
		//sql check
		updateCatalogBean.getCatalogs().forEach(catalogBean->{
			YmtCatalogsI catalogInSql = catalogsInSql.stream()
												.filter(x->x.getsCatalogId().equals(catalogBean.getCatalogId()))
												.findFirst().orElse(null);
			
			Logger.verifyNotNull(catalogInSql, "sql该数据不该为空 catalogId:"+catalogBean.getCatalogId());
			
			Logger.comment("catalogId:" + catalogBean.getCatalogId());
			Logger.verifyEquals(true, catalogInSql.getiNum().equals(catalogBean.getNum()), "sql库存");
			Logger.verifyEquals(true, catalogInSql.getfQuotePrice().doubleValue()==catalogBean.getPrice(), "sql price");
			Logger.verifyEquals(true, catalogInSql.getfVipPrice().doubleValue()==catalogBean.getVipPrice(), "sql vip price");
			Logger.verifyEquals(true, catalogInSql.getfNewCustomerPrice().doubleValue()==catalogBean.getNewCustomerPrice(), "sql new price");
			
			
			
		});
		
		
		
		
		
		if (YmtMongoDriverFactroy.isNewJavaMgoCheck()){
			NewJavaMongoCheckService.checkProductMgoSyncBySQL_NewJavaMgo(productId);
		}
	}
}
	
	
	